/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IrPanel.java
 *
 * Created on 21.12.2011., 13.54.33
 */
package aor2.gui.cpu;

import aor2.cpu.InternBus;
import aor2.cpu.blockcontrol.ControlSignalSet;
import aor2.cpu.blockregisters.InstructionRegister;
import java.awt.Graphics;

/**
 * 
 * @author Moxi
 */
public class IrPanel extends javax.swing.JPanel {

	private static IrPanel singleton = null;

	public static IrPanel getInstance() {
		if (null == singleton)
			singleton = new IrPanel();
		return singleton;
	}

	private JLabelSet ir0_out, ir1_out, ir2_out;

	private void makeSignals() {
		ir0_out = new JLabelSet(false, ir0_out1, ir0_out2);
		ir1_out = new JLabelSet(false, ir1_out1, ir1_out2);
		ir2_out = new JLabelSet(false, ir2_out1, ir2_out2);
	}

	private void refresh() {
		ControlSignalSet ctrlSS = ControlSignalSet.getInstance();
		ir0_in.setEnabled(ctrlSS.get("IR0in").isActive());
		ir1_in.setEnabled(ctrlSS.get("IR1in").isActive());
		ir2_in.setEnabled(ctrlSS.get("IR2in").isActive());
		ir0_out.setEnabled(ctrlSS.get("IR0Lout").isActive());
		ir1_out.setEnabled(ctrlSS.get("IR1out").isActive());
		ir2_out.setEnabled(ctrlSS.get("IR2out").isActive());
		ir0.setText("IR0: 0x"
				+ Long.toHexString(InstructionRegister.IR0.getState())
						.toUpperCase());
		ir1.setText("IR1: 0x"
				+ Long.toHexString(InstructionRegister.IR1.getState())
						.toUpperCase());
		ir2.setText("IR2: 0x"
				+ Long.toHexString(InstructionRegister.IR2.getState())
						.toUpperCase());
		
                //M1
                if(!InternBus.M1.isHighZ()){
                m1_1.setText("0x"
				+ Long.toHexString(InternBus.M1.getState() & 0xFFFF)
						.toUpperCase());
		m1_2.setText("0x"
				+ Long.toHexString(InternBus.M1.getState() & 0xFFFF)
						.toUpperCase());
		m1_3.setText("0x"
				+ Long.toHexString(InternBus.M1.getState() & 0xFFFF)
						.toUpperCase());
                }else{
                    m1_1.setText("Z");
                    m1_2.setText("Z");
                    m1_3.setText("Z");
                }
                if(!InternBus.M2.isHighZ()){
		m2_1.setText("0x"
				+ Long.toHexString(InternBus.M2.getState() & 0xFFFF)
						.toUpperCase());
		;
		m2_2.setText("0x"
				+ Long.toHexString(InternBus.M2.getState() & 0xFFFF)
						.toUpperCase());
		;
		m2_3.setText("0x"
				+ Long.toHexString(InternBus.M2.getState() & 0xFFFF)
						.toUpperCase());
                }else{
                    m2_1.setText("Z");
                    m2_2.setText("Z");
                    m2_3.setText("Z"); 
                }

	}

	/** Creates new form IrPanel */
	private IrPanel() {
		initComponents();
		makeSignals();
		refresh();
	}

	@Override
	protected void paintComponent(Graphics g) {
		refresh();
		super.paintComponent(g);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {
		java.awt.GridBagConstraints gridBagConstraints;

		jLayeredPane1 = new javax.swing.JLayeredPane();
		ir2_in = new javax.swing.JLabel();
		ir0_in = new javax.swing.JLabel();
		ir1_in = new javax.swing.JLabel();
		ir0_out1 = new javax.swing.JLabel();
		ir1_out1 = new javax.swing.JLabel();
		ir2_out1 = new javax.swing.JLabel();
		ir0_out2 = new javax.swing.JLabel();
		ir1_out2 = new javax.swing.JLabel();
		ir2_out2 = new javax.swing.JLabel();
		ir0 = new javax.swing.JLabel();
		ir1 = new javax.swing.JLabel();
		ir2 = new javax.swing.JLabel();
		m1_1 = new javax.swing.JLabel();
		m1_2 = new javax.swing.JLabel();
		m1_3 = new javax.swing.JLabel();
		m2_1 = new javax.swing.JLabel();
		m2_2 = new javax.swing.JLabel();
		m2_3 = new javax.swing.JLabel();
		background = new javax.swing.JLabel();

		setLayout(new java.awt.GridBagLayout());

		ir2_in.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
		ir2_in.setDisabledIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
		ir2_in.setBounds(213, 457, 70, 10);
		jLayeredPane1.add(ir2_in, javax.swing.JLayeredPane.DEFAULT_LAYER);

		ir0_in.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
		ir0_in.setDisabledIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
		ir0_in.setBounds(213, 80, 70, 10);
		jLayeredPane1.add(ir0_in, javax.swing.JLayeredPane.DEFAULT_LAYER);

		ir1_in.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
		ir1_in.setDisabledIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
		ir1_in.setBounds(213, 270, 70, 10);
		jLayeredPane1.add(ir1_in, javax.swing.JLayeredPane.DEFAULT_LAYER);

		ir0_out1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
		ir0_out1.setDisabledIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
		ir0_out1.setBounds(496, 173, 30, 10);
		jLayeredPane1.add(ir0_out1, javax.swing.JLayeredPane.DEFAULT_LAYER);

		ir1_out1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
		ir1_out1.setDisabledIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
		ir1_out1.setBounds(496, 362, 30, 10);
		jLayeredPane1.add(ir1_out1, javax.swing.JLayeredPane.DEFAULT_LAYER);

		ir2_out1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
		ir2_out1.setDisabledIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
		ir2_out1.setBounds(496, 551, 30, 10);
		jLayeredPane1.add(ir2_out1, javax.swing.JLayeredPane.DEFAULT_LAYER);

		ir0_out2.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/aor2/common/resources/vert_1pt_1.jpg"))); // NOI18N
		ir0_out2.setDisabledIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/aor2/common/resources/vert_1pt_0.jpg"))); // NOI18N
		ir0_out2.setBounds(525, 178, 10, 7);
		jLayeredPane1.add(ir0_out2, javax.swing.JLayeredPane.DEFAULT_LAYER);

		ir1_out2.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/aor2/common/resources/vert_1pt_1.jpg"))); // NOI18N
		ir1_out2.setDisabledIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/aor2/common/resources/vert_1pt_0.jpg"))); // NOI18N
		ir1_out2.setBounds(525, 367, 10, 7);
		jLayeredPane1.add(ir1_out2, javax.swing.JLayeredPane.DEFAULT_LAYER);

		ir2_out2.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/aor2/common/resources/vert_1pt_1.jpg"))); // NOI18N
		ir2_out2.setDisabledIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/aor2/common/resources/vert_1pt_0.jpg"))); // NOI18N
		ir2_out2.setBounds(525, 556, 10, 7);
		jLayeredPane1.add(ir2_out2, javax.swing.JLayeredPane.DEFAULT_LAYER);

		ir0.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		ir0.setBounds(290, 90, 110, 50);
		jLayeredPane1.add(ir0, javax.swing.JLayeredPane.DEFAULT_LAYER);

		ir1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		ir1.setBounds(290, 280, 110, 50);
		jLayeredPane1.add(ir1, javax.swing.JLayeredPane.DEFAULT_LAYER);

		ir2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		ir2.setBounds(290, 468, 110, 50);
		jLayeredPane1.add(ir2, javax.swing.JLayeredPane.DEFAULT_LAYER);
		m1_1.setBounds(150, 25, 60, 20);
		jLayeredPane1.add(m1_1, javax.swing.JLayeredPane.DEFAULT_LAYER);
		m1_2.setBounds(150, 210, 60, 20);
		jLayeredPane1.add(m1_2, javax.swing.JLayeredPane.DEFAULT_LAYER);
		m1_3.setBounds(150, 400, 60, 20);
		jLayeredPane1.add(m1_3, javax.swing.JLayeredPane.DEFAULT_LAYER);
		m2_1.setBounds(570, 165, 60, 20);
		jLayeredPane1.add(m2_1, javax.swing.JLayeredPane.DEFAULT_LAYER);
		m2_2.setBounds(570, 355, 60, 20);
		jLayeredPane1.add(m2_2, javax.swing.JLayeredPane.DEFAULT_LAYER);
		m2_3.setBounds(570, 545, 60, 20);
		jLayeredPane1.add(m2_3, javax.swing.JLayeredPane.DEFAULT_LAYER);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		add(jLayeredPane1, gridBagConstraints);

		background.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/aor2/common/resources/IR.jpg"))); // NOI18N
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		add(background, gridBagConstraints);
	}// </editor-fold>//GEN-END:initComponents
		// Variables declaration - do not modify//GEN-BEGIN:variables

	private javax.swing.JLabel background;
	private javax.swing.JLabel ir0;
	private javax.swing.JLabel ir0_in;
	private javax.swing.JLabel ir0_out1;
	private javax.swing.JLabel ir0_out2;
	private javax.swing.JLabel ir1;
	private javax.swing.JLabel ir1_in;
	private javax.swing.JLabel ir1_out1;
	private javax.swing.JLabel ir1_out2;
	private javax.swing.JLabel ir2;
	private javax.swing.JLabel ir2_in;
	private javax.swing.JLabel ir2_out1;
	private javax.swing.JLabel ir2_out2;
	private javax.swing.JLayeredPane jLayeredPane1;
	private javax.swing.JLabel m1_1;
	private javax.swing.JLabel m1_2;
	private javax.swing.JLabel m1_3;
	private javax.swing.JLabel m2_1;
	private javax.swing.JLabel m2_2;
	private javax.swing.JLabel m2_3;
	// End of variables declaration//GEN-END:variables
}
